package com.finance.logistics.anlyzier;

import com.finance.framework.lang.Privilege;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.report.bean.pojo.LogisticsExtend;
import com.finance.report.service.ILogisticsDataService;
import com.finance.report.service.ILogisticsErrorLogService;
import com.finance.report.service.ILogisticsExtendService;
import com.finance.system.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 *
 * Created by CBWL on 2017/4/27.
 */
@Component
public abstract class AbstractHandler {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected ILogisticsDataService logisticsDataService;
    @Autowired
    protected IOrganizationService organizationService;
    @Autowired
    protected ILogisticsErrorLogService logisticsErrorLogService;
    @Autowired
    protected ILogisticsExtendService logisticsExtendService;
    @Autowired
    protected ILogisticsMappingService logisticsMappingService;
    @Autowired
    protected IUserService userService;
    @Autowired
    protected ISupplierService supplierService;
    @Autowired
    protected IRegionCodeService regionCodeService;


    protected void updateDataVild(LogisticsData logisticsData,Privilege privilege) throws Exception {
        Date signTime = logisticsData.getSignTime();
        LogisticsExtend logisticsExtend = this.logisticsExtendService.getByWayBillNum(signTime, logisticsData.getWaybillNum());
        String from = logisticsExtend.getDataValid();
        String dataValid = HandlerHelper.parse(logisticsExtend.getDataValid(), privilege);
        logisticsExtend.setDataValid(dataValid);
        this.logger.info("开始更新运单{}状态标记 from{} to{} ", new String[]{logisticsData.getWaybillNum(),from, logisticsExtend.getDataValid()});
        this.logisticsExtendService.updateWithdataValid(signTime, logisticsExtend, from);
    }
    protected void updateDataVild(LogisticsData logisticsData, List<Privilege> privilegeList) throws Exception {
        if(!CollectionUtils.isEmpty(privilegeList)){
            LogisticsExtend logisticsExtend = this.logisticsExtendService.getByWayBillNum(logisticsData.getSignTime(), logisticsData.getWaybillNum());
            String from = logisticsExtend.getDataValid();
            String dataValid = HandlerHelper.parse(logisticsExtend.getDataValid(), privilegeList);
            logisticsExtend.setDataValid(dataValid);
            this.logisticsExtendService.updateWithdataValid(logisticsData.getSignTime(), logisticsExtend,from);
            this.logger.info("更新运单状态标记 from{} to{} ", new String[]{from, logisticsExtend.getDataValid()});
        }
    }

    private AbstractHandler nextHandler;

    public AbstractHandler getNextHandler(){
        if(this.nextHandler != null) {
            return this.nextHandler;
        }
        return new AbstractHandler() {
            @Override
            public void doHandler(LogisticsData logisticsData) {
                //do nothing
            }
        };
    }

    public void setNextHandler(AbstractHandler nextHandler){
        this.nextHandler = nextHandler;
    }
    public abstract void doHandler(LogisticsData logisticsData);
}
